<template>
  <div class="recharge-rule">
    <el-form label-width="90px" :model="rechargeForm" :rules="rules" ref="ruleForm">
      <el-form-item label="充值金额1:" prop="one">
        <el-input v-model="rechargeForm.one" >
          <template slot="append">元</template>
        </el-input>
      </el-form-item>

      <el-form-item label="充值金额2:" prop="two">
        <el-input v-model="rechargeForm.two">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>

      <el-form-item label="充值金额3:" prop="three">
        <el-input v-model="rechargeForm.three">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>

      <el-form-item label="充值金额4:" prop="four">
        <el-input v-model="rechargeForm.four">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="cancle">取消</el-button>
      <el-button type="primary" @click="submit('ruleForm')">确定</el-button>
    </p>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        rechargeForm: {
          one: '',
          two: '',
          three: '',
          four: ''
        },
        rules: {
          one: [
            { required: true, message: '请输入充值金额', trigger: 'blur' },
            { pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于0的数值', trigger: ['blur', 'change'] },
          ],
          two: [
            { required: true, message: '请输入充值金额', trigger: 'blur' },
            { pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于0的数值', trigger: ['blur', 'change'] },
          ],
          three: [
            { required: true, message: '请输入充值金额', trigger: 'blur' },
            { pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于0的数值', trigger: ['blur', 'change'] },
          ],
          four: [
            { required: true, message: '请输入充值金额', trigger: 'blur' },
            { pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入大于0的数值', trigger: ['blur', 'change'] },
          ],
        }
      }
    },
    mounted() {
		this.getRechargeRule();
    },
    methods: {
		getRechargeRule() {
			this.$axios({
				method: 'post',
				url: '/promotion/recharge-rule/getRechargeRule'
			}).then((res) => {
        if(res.data.length) {
          this.rechargeForm.one = res.data[0];
          this.rechargeForm.two = res.data[1];
          this.rechargeForm.three = res.data[2];
          this.rechargeForm.four = res.data[3];
        }
			})
		},
    cancle() {
      this.$emit('closeDialog',false);
    },
    submit(rule) {
      let rechargeData = [];
      rechargeData.push(this.rechargeForm.one)
      rechargeData.push(this.rechargeForm.two)
      rechargeData.push(this.rechargeForm.three)
      rechargeData.push(this.rechargeForm.four)
      this.$refs[rule].validate((valid) => {
        if(valid) {
          this.$axios({
            method: 'post',
            url: '/promotion/recharge-rule/updateRechargeRule',
            data: this.$qs.stringify({recharges:rechargeData}, { arrayFormat: 'repeat' })
          }).then(res => {
            if(res.code == 200) {
              this.$message.success('设置规则成功');
              this.$emit('closeDialog',false)
            }
          })
        } else {
          return false
        }
      })
    }
    }
  }
</script>

<style scoped>
  .el-form {
    text-align: center;
  }
  .el-form-item {
    display: inline-block;
  }
  .el-input {
    width: 200px;
  }



</style>
